﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using SaleManagement.Web.App_Code;
using SaleManagement.Services;
using SaleManagement.Domains;

namespace SaleManagement.Web.Pages
{
  public partial class ExportList : BasePage
  {
    #region Variables and properties
    private const string SEARCH_FROMDATE = "ExportList_Search_FromDate";
    private const string SEARCH_TODATE = "ExportList_Search_ToDate";

    private string _innerError = string.Empty;

    #endregion

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack && !IsCallback)
      {
        deFromDate.Date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
        deToDate.Date = DateTime.Now;

        #region Session
        if (GetSessionSearch(SEARCH_FROMDATE) != null)
        {
          if (GetSessionSearch(SEARCH_FROMDATE) != string.Empty)
          {
            deFromDate.Date = Convert.ToDateTime(GetSessionSearch(SEARCH_FROMDATE));
          }
          else
          {
            deFromDate.Value = null;
          }
        }

        if (GetSessionSearch(SEARCH_TODATE) != null)//not in session
        {
          if (GetSessionSearch(SEARCH_TODATE) != string.Empty)//empty value
          {
            deToDate.Date = Convert.ToDateTime(GetSessionSearch(SEARCH_TODATE));
          }
          else
          {
            deToDate.Value = null;
          }
        }
        #endregion

        LoadExport();
      }
    }

    protected void gvExport_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
    {
      string[] para = e.Parameters.Split('|');
      if (para[0] == "Delete")
      {
        _innerError = DeleteExport(Convert.ToInt32(para[1]));
      }

      LoadExport();
    }

    protected void gvExport_CustomJSProperties(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewClientJSPropertiesEventArgs e)
    {
      e.Properties["cpInnerError"] = _innerError;
    }
    #endregion

    #region Methods
    private void LoadExport()
    {
      DateTime? fromDate = deFromDate.Value as DateTime?;
      DateTime? toDate = deToDate.Value as DateTime?;

      ConfigSessionSearch(SEARCH_FROMDATE, fromDate.HasValue ? fromDate.Value.ToString() : string.Empty);
      ConfigSessionSearch(SEARCH_TODATE, toDate.HasValue ? toDate.Value.ToString() : string.Empty);

      gvExport.DataSource = new ExportService().Search(fromDate, toDate);
      gvExport.DataBind();
    }

    private string DeleteExport(int id)
    {
      #region Check detail
      TList<ExportDetail> detailList = new ExportDetailService().GetByExportId(id);
      if (detailList.Count > 0)
      {
        return "Không thể xóa hóa đơn này.";
      }
      #endregion

      #region Delete DB
      try
      {
        new ExportService().Delete(id);
        return string.Empty;
      }
      catch (Exception ex)
      {
        WriteLog("ExportList::DeleteExport", ex);
        return "Có lỗi trong quá trình xóa dữ liệu";
      }
      #endregion
    }
    #endregion
  }
}
